package com.shunhao.widgets;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;

/**
 * @author Administrator
 */
public class GridDivider extends RecyclerView.ItemDecoration {

    /**
     * 中间间距
     */
    private int centerSpacing;

    /**
     * 周围间距
     */
    private int aroundSpacing;

    /**
     * 左间距
     */
    private int leftSpacing;

    /**
     * 右间距
     */
    private int rightSpacing;

    /**
     * 默认间距
     */
    private int defaultSpacing = 9;

    /**
     * 条目类型
     */
    private int itemType = -1;

    /**
     * streaming
     *
     * @return 返回对象
     */
    public static GridDivider createSteamingDivider() {
        return new GridDivider(15, 15, 8);
    }


    public GridDivider(int left, int right, int center) {
        leftSpacing = ConvertUtils.dp2px(left);
        rightSpacing = ConvertUtils.dp2px(right);
        centerSpacing = center;
    }


    public GridDivider(int around) {
        leftSpacing = rightSpacing = ConvertUtils.dp2px(around);
        centerSpacing = 0;
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (itemType == -1) {
            StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            int spanIndex = layoutParams.getSpanIndex();
            outRect.top = ConvertUtils.dp2px(11);
            if (spanIndex == 0) {
                outRect.left = leftSpacing;
                outRect.right = rightSpacing / 2;
            } else {
                outRect.right = rightSpacing;
                outRect.left = leftSpacing / 2;
            }
        }
    }
}
